<?php

require_once dirname(__FILE__) . '/../lib/ct_documentoGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/ct_documentoGeneratorHelper.class.php';

/**
 * ct_documento actions.
 *
 * @package    ingenyo-biblio
 * @subpackage ct_documento
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class ct_documentoActions extends autoCt_documentoActions
{

  public function preExecute()
  {
//    var_dump($this->getUser()->getIdPersonaUcalp());    
    if ($this->getUser()->hasAttribute('loged'))
    {
      if (!$this->getUser()->chequearLogOut())
      {

        $this->getUser()->clearAllAttribute();
        $this->forward('ct_documento', 'index');
      }
    }
    parent::preExecute();

  }

  public function executeIndex(sfWebRequest $request)
  {

    parent::executeIndex($request);
    $this->forward404Unless($this->getUser()->estoyLogeado($request), 'debe estar logeado en zona interactiva para acceder aqui');
    $this->quicksearch = $request->getParameter('quicksearch', false);
    $this->filters = new CtDocumentoFormFilterCollection();

  }

  public function executeNew(sfWebRequest $request)
  {
    $this->redirect('ct_documento/index');

  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->redirect('ct_documento/index');

  }

  public function executeDelete(sfWebRequest $request)
  {
    $this->redirect('ct_documento/index');

  }

  public function executeBuscar(sfWebRequest $request)
  {

    $form = new CtDocumentoFormFilterCollection();
    $form->bind($request->getParameter('filters'));
    $pager = new sfPropelPager('CTDocumento', sfConfig::get('app_max_documentos_en_busqueda'));
    $pager->setCriteria($form->getCriteriaFilter());
    $pager->setPage($request->getParameter('page', 1));

    $pager->init();

    $this->sort = $this->getSort();
    $this->helper = new ct_documentoGeneratorHelper();
    sfConfig::set('sf_web_debug', false);
    sfContext::getInstance()->getConfiguration()->loadHelpers('Partial');

    $this->generarLog($request);
    return $this->renderText(get_partial('result_list', array('pager' => $pager,
          'sort' => $this->sort,
          'configuration' => $this->configuration,
          'helper' => $this->helper)));

  }

  public function executeAddCustom(sfWebRequest $request)
  {
    $next = $request->getParameter('next');
    $is_the_last = $request->getParameter('is_the_last');
    $form = new CtDocumentoFormFilterCollection();
    $form->addCustom($next, $is_the_last);
    sfConfig::set('sf_web_debug', false);
    sfContext::getInstance()->getConfiguration()->loadHelpers('Partial');
    return $this->renderText(get_partial('custom', array('custom_filters' => $form['filters']['custom'][$next],
          'add' => true)));

  }

  public function executeShow(sfWebRequest $request)
  {
    $this->CtDocumento = $this->getRoute()->getObject();
    $this->getUser()->setAttribute('id_documento', $this->CtDocumento->getId());
    $this->caracteristicas = $this->CtDocumento->getCaracteristicasFicha();
    $this->caracteristicasAACR2 = $this->CtDocumento->getCaracteristicasFichaAACR2();
    $this->contenido = $this->CtDocumento->getContenido();

  }

  public function executeReservarLibro(sfWebRequest $request)
  {
    $id_documento = $request->getParameter('id');
    $documento = CtDocumentoPeer::retrieveByPK($id_documento);


    $persona_sin_castear = BbPersonaPeer::retrieveUcalpPersona($this->getUser()->getAttribute('person_id'));
    $persona = BbPersonaPeer::getPersona($persona_sin_castear->getId());
    
    $this->BbReservaLibro = new Bbreservalibro();
    $libro =$documento->getLibroBySucursalUser();
    $this->BbReservaLibro->setIdlibro($libro->getId());
    $this->BbReservaLibro->setBbPersona($persona);
    $this->BbReservaLibro->setFechaGenera(time());
    $this->BbReservaLibro->setfechaVencimiento(strtotime('+' . $persona->getMaxDiasReserva() . ' days', time()));
    #Cambiamos el del libro estado a reservado
    $this->BbReservaLibro->save();
    
    #Guardo los cabios del libro
    $libro->setEstadoReservado();
    $libro->save();

    $this->setLayout('layout_iframe');

  }

  public function executeGetComentarios(sfWebRequest $request)
  {
    $this->redirect('ct_documento_comentario/new');

  }

  public function executeSaveVoto(sfWebRequest $request)
  {
    $voto = (integer) $request->getParameter('valor');
    $id_documento = $this->getUser()->getAttribute('id_documento');
    $persona = BbPersonaPeer::retrieveUcalpPersona($this->getUser()->getIdPersonaUcalp());
    if (($voto > 0) && ($voto < 6) && ($persona->puedeVotarDocumento($id_documento) == 'si'))
    {
      CtDocumentoValoracionPeer::crearVoto($id_documento, $persona, $voto);
    }

    return $this->renderPartial('rating', array('CtDocumento' => CtDocumentoPeer::retrieveByPK($id_documento)));

  }

  private function generarLog($request)
  {
    if (LogPeer::getTermino($request->getParameter('filters')) != '')
    {
      $log = new Log();
      $filter = $request->getParameter('filters');
      $log->setTipoBusqueda($filter['tipo_busqueda']);
      $log->setTermino(LogPeer::getTermino($request->getParameter('filters')));
      $log->setAplicacion('zonainteractiva');
      $log->setIdpersona($this->getUser()->getIdPersona());
      $log->save();
    }

  }
  
  public function executeGetTerminos(sfWebRequest $request){
    $params = $request->getParameter('filters');
    
    $json['terminosMasBuscado'] = LogPeer::geterminosMasBuscado(10);
    $json['personaQueBuscoEsteTermino'] = LogPeer::getPersonaQueBuscoEsteTerminoTambienBusco($params['valor_simple']);
    
    return $this->renderText(json_encode($json));
  }

}
